package com.dikeleakagedetection.myapplication.ui.dataMonitor

import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.dikeleakagedetection.myapplication.DikeLeakageDetectionApp
import com.dikeleakagedetection.myapplication.databinding.DlgSelectTabletBinding
import com.dikeleakagedetection.myapplication.logic.dao.AppDatabase
import com.dikeleakagedetection.myapplication.logic.model.TabletSimple
import kotlin.concurrent.thread

class SelectTabletDialogFragment : DialogFragment() {
    private lateinit var binding: DlgSelectTabletBinding

    private var clickCallBack: ClickCallBack? = null

    private val tabletSimpleList = ArrayList<TabletSimple>()
    private val tabletDao = AppDatabase.getDatabase(DikeLeakageDetectionApp.context).tabletDao()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = DlgSelectTabletBinding.inflate(inflater, container, false)
        dialog!!.setCanceledOnTouchOutside(false)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initWindow()
        initData()
        initView()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)

    }

    private fun initData() {
        thread {
            val loadAllTablets = tabletDao.loadAllTablets()
            for (tablet in loadAllTablets) {
                tabletSimpleList.add(
                    TabletSimple(
                        tablet.id,
                        tablet.place,
                        tablet.date,
                        tablet.length!!,
                        tablet.width!!,
                        false
                    )
                )
            }

            activity?.runOnUiThread {
                val layoutManager = LinearLayoutManager(context)
                val tabletAdapter = TabletSimpleAdapter(this, tabletSimpleList)
                tabletAdapter.setOnItemClickListener {
                    tabletAdapter.notifyDataSetChanged()
                }
                binding.rvTabletListDlgSelectTablet.adapter = tabletAdapter
                binding.rvTabletListDlgSelectTablet.layoutManager = layoutManager
            }
        }
    }

    private fun initView() {

        binding.tvConfirmDigSelectTablet.setOnClickListener {
            var tabletId = -1L
            for(tablet in tabletSimpleList){
                if(tablet.isChecked){
                    tabletId = tablet.id
                    break
                }
            }
            clickCallBack?.clickConfirm(tabletId)
            dismiss()
        }
        binding.tvCancelDigSelectTablet.setOnClickListener {
            dismiss()
        }
    }

    private fun initWindow() {
        //初始化window相关
        val window = dialog?.window //获取window对象
        window?.apply {
            setBackgroundDrawable(ColorDrawable(Color.WHITE))//设备背景为透明（默认白色）
            attributes.width = 1200
            attributes.gravity = Gravity.CENTER

        }
    }

    fun setClickCallBack(clickCallBack: ClickCallBack) {
        this.clickCallBack = clickCallBack
    }

    interface ClickCallBack {
        fun clickConfirm(tabletId: Long)
    }
}